package middleware

import (
	"errors"
	"robotDaBai/app/common/dao"
	DtoRobot "robotDaBai/app/common/dto/robot"

	//"github.com/e421083458/gin_scaffold/dto"
	//"robotDaBai/app/common/dto/robot"
	//"github.com/e421083458/gin_scaffold/dto"
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
)

//session身份校验中间件
func SessionAuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		session := sessions.Default(c)
		if name,ok:=session.Get("user").(string);!ok||name==""{
			ResponseError(c, InternalErrorCode, errors.New("user not login"))
			c.Abort()
			return
		}
		c.Next()
	}
}



//通过qq 获取用户信息
func Authenticator() gin.HandlerFunc{
	return func(this *gin.Context) {
		params := &DtoRobot.AuthInput{}
		if err:=params.BindingValidParams(this);err!=nil{
			ResponseError(this,100,err)
			return
		}
		//UserInfo := &dao.User{}
		UserInfo,err:=(&dao.User{}).GetInfo(this,params)
		if err!=nil{
			ResponseError(this,101,err)
		}
		// 将用户信息存储起来
		this.Set("userInfo",UserInfo)
		this.Next()
	}
}
